/*
 * Copyright 2011 Stefan Ferstl
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.debmavenplugin.archiver;

import java.io.File;
import java.util.Set;

import org.apache.commons.io.FileUtils;
import org.junit.Before;

import com.google.code.debmavenplugin.archiver.ArchiveMetaData;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

/**
 * Some helpful methods to test {@link ArchiveMetaData} and its subclasses.
 */
public abstract class AbstractArchiveMetaDataTst<T extends ArchiveMetaData> {

  private T metaData;

  /**
   * Setup some metadata for testing.
   */
  @Before
  public void setUp() {
    this.metaData = createDefaultMetadata();
  }

  /**
   * Create meta data used for testing.
   * @return The created metadata.
   */
  protected abstract T createDefaultMetadata();

  /**
   * Returns the metadata.
   * @return The metadata.
   */
  protected T getMetaData() {
    return this.metaData;
  }

  /**
   * Checks that the given file is excluded from the metadata.
   * @param file The file to check.
   */
  protected void assertExclusion(String file) {
    File f = new File(getMetaData().getBaseDirectory(), file);
    assertTrue("File '" + f.getPath() + "' is not excluded", getMetaData().isExcluded(f));
  }

  /**
   * Checks that the given file is included in the metadata.
   * @param file The file to check.
   */
  protected void assertInclusion(String file) {
    File f = new File(getMetaData().getBaseDirectory(), file);
    assertFalse("File '" + f.getPath() + "' is not included", getMetaData().isExcluded(f));
  }

  /**
   * Lists all included files using a {@link FileCollectingFileFilter}.
   * @return All files included in the metadata.
   */
  protected Set<File> listFiles() {
    FileCollectingFileFilter fileFilter = new FileCollectingFileFilter(getMetaData(), false);
    FileUtils.listFiles(getMetaData().getBaseDirectory(), fileFilter, fileFilter);
    Set<File> files = fileFilter.getCollectedFiles();
    return files;
  }

}
